package com.hissy.cn.lock;

import com.alibaba.fastjson.JSON;

/**
 * Created by zhangyanfei on 2019/1/31.
 * redis 创建工厂
 * @author zhangyanfei
 * @date 2019/01/31
 */
public abstract class AbstractJedisLockFactory {
    protected String namespace;
    protected String keyPrefix;
    protected int expiryTimeMillis;
    public static final String LOCK_KEY_SPLITOR="/";

    protected AbstractJedisLockFactory(String namespace, String keyPrefix){
        this(namespace,keyPrefix,JedisLock.DeFault_EXPIRY_TIME_MILLIS);
    }

    protected AbstractJedisLockFactory(String namespace,String keyPrefix,int expiryTimeMillis){
        this.namespace=namespace;
        this.expiryTimeMillis=expiryTimeMillis;
        this.keyPrefix=keyPrefix;
    }

    /**
     * 锁的构建
     * @param params
     * @return
     */
    public JedisLock buildLock(Object... params){
        String identity= JSON.toJSONString(params);
        return new JedisLock(namespace,lockKeyPostProcess(identity),expiryTimeMillis);
    }


    /**
     * lockKey构建后置处理，推荐进行key业务属性
     *
     * @param identify lockKey标识
     * @return
     */
    protected String lockKeyPostProcess(String identify) {
        return keyPrefix + LOCK_KEY_SPLITOR + identify;
    }
}
